// pages/coupon/coupon.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign: "领取",
    navState: 0, //导航状态
  },
  //点击导航
  navSwitch: function (e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.setData({
      navState: index
    })
  },
  coupon(e) {
    var id = e.currentTarget.dataset.id
    var couponname = e.currentTarget.dataset.couponname
    var coupontype = e.currentTarget.dataset.coupontype
    var priceone = e.currentTarget.dataset.priceone
    var pricetwo = e.currentTarget.dataset.pricetwo
    var number = e.currentTarget.dataset.number
    var startdate = e.currentTarget.dataset.startdate
    var enddate = e.currentTarget.dataset.enddate
    var rule = e.currentTarget.dataset.rule
    console.log(rule)
    var name = getApp().globalData.usersname
    var that = this;

    if (number > 0) {
      console.log('库存充足！')
      wx.request({
        url: 'http://localhost/shopping/coupon_if.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          couponid: id,
          username: name,
        },
        success: function (res) {
          // console.log(res.data)
          if (res.data == '优惠券已存在') {
            console.log('优惠券已存在!!')
            wx.showModal({
              title: '',
              content: '优惠券已经领取了!',
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            console.log('优惠券不存在!!')
            wx.request({
              url: 'http://localhost/shopping/users.php',
              header: {
                'Content-Type': 'application/json'
              },
              data: {
                name: name
              },
              success: function (res) {
                // console.log(res.data)
                var startdatetime = Date.parse(startdate) / 1000
                var enddatetime = Date.parse(enddate) / 1000
                var time = util.formatTime(new Date());
                var nowtime = Date.parse(time) / 1000
                console.log(startdatetime, nowtime, enddatetime)
                if (nowtime >= startdatetime && nowtime <= enddatetime) {
                  console.log('时间在规定范围内，可以领取')
                  if (res.data.users[0].vip == 'vip3') {
                    if (rule == '所有' || rule == 'vip1' || rule == 'vip2' || rule == 'vip3') {
                      console.log('满足领取要求!')
                      wx.request({
                        url: 'http://localhost/shopping/coupon_edit.php',
                        header: {
                          'Content-Type': 'application/json'
                        },
                        data: {
                          id: id,
                          number: number,
                        },
                        success: function (res) {
                          console.log(res.data)
                        },
                        fail: function (res) {
                          console.log("fail");
                        }
                      })
                      wx.request({
                        url: 'http://localhost/shopping/user_coupon_add.php',
                        header: {
                          'Content-Type': 'application/json'
                        },
                        data: {
                          couponid: id,
                          username: name,
                          couponname: couponname,
                          coupontype: coupontype,
                          priceone: priceone,
                          pricetwo: pricetwo,
                          startdate: startdate,
                          enddate: enddate,
                          rule: rule,
                        },
                        success: function (res) {
                          console.log(res.data)
                          that.Coupon();
                          wx.showToast({
                            title: '领取成功',
                            icon: 'success',
                            duration: 2000
                          })
                          that.setData({

                          })
                        },
                        fail: function (res) {
                          console.log("fail");
                        }
                      })
                    } else {
                      console.log('不满足领取要求!')
                      wx.showModal({
                        title: '',
                        content: '不满足领取要求!',
                      })
                    }
                  } else if (res.data.users[0].vip == 'vip2') {
                    if (rule == '所有' || rule == 'vip1' || rule == 'vip2') {
                      console.log('满足领取要求!').
                      wx.request({
                        url: 'http://localhost/shopping/coupon_edit.php',
                        header: {
                          'Content-Type': 'application/json'
                        },
                        data: {
                          id: id,
                          number: number,
                        },
                        success: function (res) {
                          console.log(res.data)
                        },
                        fail: function (res) {
                          console.log("fail");
                        }
                      })
                      wx.request({
                        url: 'http://localhost/shopping/user_coupon_add.php',
                        header: {
                          'Content-Type': 'application/json'
                        },
                        data: {
                          couponid: id,
                          username: name,
                          couponname: couponname,
                          coupontype: coupontype,
                          priceone: priceone,
                          pricetwo: pricetwo,
                          startdate: startdate,
                          enddate: enddate,
                          rule: rule,
                        },
                        success: function (res) {
                          console.log(res.data)
                          that.Coupon();
                          wx.showToast({
                            title: '领取成功',
                            icon: 'success',
                            duration: 2000
                          })
                          that.setData({

                          })
                        },
                        fail: function (res) {
                          console.log("fail");
                        }
                      })
                    } else {
                      console.log('不满足领取要求!')
                      wx.showModal({
                        title: '',
                        content: '不满足领取要求!',
                      })
                    }
                  } else if (res.data.users[0].vip == 'vip1') {
                    if (rule == '所有' || rule == 'vip1') {
                      console.log('满足领取要求!')
                      wx.request({
                        url: 'http://localhost/shopping/coupon_edit.php',
                        header: {
                          'Content-Type': 'application/json'
                        },
                        data: {
                          id: id,
                          number: number,
                        },
                        success: function (res) {
                          console.log(res.data)
                        },
                        fail: function (res) {
                          console.log("fail");
                        }
                      })
                      wx.request({
                        url: 'http://localhost/shopping/user_coupon_add.php',
                        header: {
                          'Content-Type': 'application/json'
                        },
                        data: {
                          couponid: id,
                          username: name,
                          couponname: couponname,
                          coupontype: coupontype,
                          priceone: priceone,
                          pricetwo: pricetwo,
                          startdate: startdate,
                          enddate: enddate,
                          rule: rule,
                        },
                        success: function (res) {
                          console.log(res.data)
                          that.Coupon();
                          wx.showToast({
                            title: '领取成功',
                            icon: 'success',
                            duration: 2000
                          })
                        },
                        fail: function (res) {
                          console.log("fail");
                        }
                      })

                    } else {
                      console.log('不满足领取要求!')
                      wx.showModal({
                        title: '',
                        content: '不满足领取要求!',
                      })
                    }
                  } else if (!res.data.users[0].vip) {
                    if (rule == '所有') {
                      console.log('满足领取要求!')
                      wx.request({
                        url: 'http://localhost/shopping/coupon_edit.php',
                        header: {
                          'Content-Type': 'application/json'
                        },
                        data: {
                          id: id,
                          number: number,
                        },
                        success: function (res) {
                          console.log(res.data)
                        },
                        fail: function (res) {
                          console.log("fail");
                        }
                      })
                      wx.request({
                        url: 'http://localhost/shopping/user_coupon_add.php',
                        header: {
                          'Content-Type': 'application/json'
                        },
                        data: {
                          couponid: id,
                          username: name,
                          couponname: couponname,
                          coupontype: coupontype,
                          priceone: priceone,
                          pricetwo: pricetwo,
                          startdate: startdate,
                          enddate: enddate,
                          rule: rule,
                        },
                        success: function (res) {
                          console.log(res.data)
                          that.Coupon();
                          wx.showToast({
                            title: '领取成功',
                            icon: 'success',
                            duration: 2000
                          })
                          that.setData({

                          })
                        },
                        fail: function (res) {
                          console.log("fail");
                        }
                      })
                    } else {
                      console.log('不满足领取要求!')
                      wx.showModal({
                        title: '',
                        content: '不满足领取要求!',
                      })
                    }
                  }
                } else {
                  console.log('时间不在规定范围内，不可以领取')
                  wx.showModal({
                    title: '',
                    content: '领取时间不在规定范围内!',
                  })
                }
                that.setData({})
              },
              fail: function (res) {
                console.log("fail");
              }
            })
          }

          that.setData({})
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    } else {
      console.log('库存不足')
      wx.showModal({
        title: '',
        content: '该优惠券已经领取完了',
      })
    }

  },

  Coupon: function () {
    let name = getApp().globalData.usersname
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/coupon.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        // console.log(res.data)
        that.setData({
          arrcoupon: res.data.coupon,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/coupon_search.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: name
      },
      success: function (res) {
        // console.log(res.data)
        that.setData({
          arrcouponextend: res.data.couponextend,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Coupon();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})